#include <string>
#include <QString>
#include <QInputDialog>
#include <QDir>
#include <QtGui>
#include "rougetradersgui_two.h"
#include "GameInfo.h"
#include "Commodity.h"

rougeTradersGUI_two::rougeTradersGUI_two(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);
	set_connections();
}

void rougeTradersGUI_two::init()
{
	init_player();
	init_states();
	init_days();
	set_day();
}

void rougeTradersGUI_two::set_connections()
{
	connect(ui.pushButtonCity, SIGNAL(clicked()), this, SLOT(change_city()));
	connect(ui.pushButtonBuy, SIGNAL(clicked()), this, SLOT(buy()));
	connect(ui.pushButtonSell, SIGNAL(clicked()), this, SLOT(sell()));
}

void rougeTradersGUI_two::put_pointer(GameInfo *pGI)
{
	pGameInfo = pGI;
}


void rougeTradersGUI_two::init_player(void)
{
	bool ok;
	QString text = QInputDialog::getText(this, tr("Name"), tr("User name:"), QLineEdit::Normal, QDir::home().dirName(), &ok);
	if (ok && !text.isEmpty()) 
	{ 
		float cash_amount;
		ui.label_Player->setText(text);
		pGameInfo->init_player(text.toStdString());
		cash_amount = pGameInfo->get_current_player()->get_cash();
		QString qstr = QString::number(cash_amount, 'f', 2);
		ui.label_Cash->setText("$" + qstr);
	}
}

void rougeTradersGUI_two::init_days()
{
	bool ok;
	// TODO: make slider or spin box
	QString text = QInputDialog::getText(this, tr("Days"), tr("How many days do you want to play for:"), QLineEdit::Normal, QDir::home().dirName(), &ok);
	if (ok && !text.isEmpty())
	{
		pGameInfo->set_day_limit(text.toInt());
	}
}

void rougeTradersGUI_two::init_states()
{
	for (int x=0; x < pGameInfo->get_total_states(); x++)
	{
		QString qstr = QString::fromStdString(pGameInfo->get_state_index(x)->get_name());
		new QListWidgetItem(qstr, ui.listWidgetCity, 0);
	}
	set_market_current_city();
}

void rougeTradersGUI_two::change_city()
{
	int row_index = ui.listWidgetCity->currentRow();
	if (ui.listWidgetCity->count() > 1)
	{
		std::string sn = pGameInfo->set_current_state_index(row_index);
		QString qstr = QString::fromStdString(sn);
		ui.label_City->setText(qstr);
	}
	set_day();
}

void rougeTradersGUI_two::set_day()
{
	pGameInfo->next_day();
	int day = pGameInfo->get_day();
	int day_limit = pGameInfo->get_day_limit();
	std::string days_str = std::to_string(static_cast<long long>(day)) + " / " + std::to_string(static_cast<long long>(day_limit)) ;
	QString qstr = QString::fromStdString( days_str );
	ui.label_Day->setText(qstr);
	set_market_current_city();
}

void rougeTradersGUI_two::set_market_current_city(void)
{
	while (ui.tableWidgetMarket->rowCount() > 0)
	{
		ui.tableWidgetMarket->removeRow(0);
	}
	for (int i = 0; i < 8; i++)
	{
		State* pState = pGameInfo->get_state_current();
		Commodity comm = pState->get_market_index(i);

		std::string sn = comm.get_name();
		QString qstr = QString::fromStdString(sn);
		QTableWidgetItem *nameItem = new QTableWidgetItem();
		nameItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
		nameItem->setText(qstr);

		QString q_current_cash = QString::number(pGameInfo->get_state_current()->get_market_index(i).get_price(), 'f', 2);
		QTableWidgetItem *priceItem = new QTableWidgetItem();
		priceItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
		priceItem->setText(q_current_cash);

		int row = ui.tableWidgetMarket->rowCount();

		ui.tableWidgetMarket->insertRow(row);
		ui.tableWidgetMarket->setItem(row, 0, nameItem);
		ui.tableWidgetMarket->setItem(row, 1, priceItem);
	}
	ui.label_City->setText(QString::fromStdString(pGameInfo->get_state_current()->get_name()));
}

void rougeTradersGUI_two::buy(void)
{
	// diaglog with sliders 0 - what can i afford
	
}

void rougeTradersGUI_two::sell(void)
{
}

rougeTradersGUI_two::~rougeTradersGUI_two()
{
}
